package com.lxmes.jiesuan.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.lxmes.business.domain.CcChukuMain;
import com.lxmes.business.domain.CcOrderInfo;
import com.lxmes.business.domain.CcRukuMain;
import com.lxmes.business.mapper.CcChukuMainMapper;
import com.lxmes.business.mapper.CcOrderInfoMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.jiesuan.mapper.JiesuanShoukuanMapper;
import com.lxmes.jiesuan.domain.JiesuanShoukuan;
import com.lxmes.jiesuan.service.IJiesuanShoukuanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收款记账Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-09-26
 */
@Service
public class JiesuanShoukuanServiceImpl implements IJiesuanShoukuanService 
{
    @Autowired
    private JiesuanShoukuanMapper jiesuanShoukuanMapper;
    @Autowired
    private CcChukuMainMapper chukuMainMapper;
    @Autowired
    private CcOrderInfoMapper ccOrderInfoMapper;

    /**
     * 查询收款记账
     * 
     * @param id 收款记账主键
     * @return 收款记账
     */
    @Override
    public JiesuanShoukuan selectJiesuanShoukuanById(Long id)
    {
        return jiesuanShoukuanMapper.selectJiesuanShoukuanById(id);
    }

    /**
     * 查询收款记账列表
     * 
     * @param jiesuanShoukuan 收款记账
     * @return 收款记账
     */
    @Override
    public List<JiesuanShoukuan> selectJiesuanShoukuanList(JiesuanShoukuan jiesuanShoukuan)
    {
        return jiesuanShoukuanMapper.selectJiesuanShoukuanList(jiesuanShoukuan);
    }

    /**
     * 新增收款记账
     * 
     * @param jiesuanShoukuan 收款记账
     * @return 结果
     */
    @Override
    @Transactional
    public int insertJiesuanShoukuan(JiesuanShoukuan jiesuanShoukuan)
    {
        Map<String, String> map = jiesuanShoukuanMapper.selectTjShowKuanByYwId(jiesuanShoukuan.getYwId());
            BigDecimal meoney=jiesuanShoukuan.getSkMoney().add(jiesuanShoukuan.getYhMoney());
            BigDecimal sk= jiesuanShoukuan.getSkMoney();
            BigDecimal yh= jiesuanShoukuan.getYhMoney();
            BigDecimal ys= jiesuanShoukuan.getYsMoney();
            if(meoney.compareTo(ys)==1){
                throw new ServiceException("收款金额加优惠金额大于应收金额！");
            }
            if(yh.compareTo(ys)==1){
                throw new ServiceException("优惠金额大于应收金额！");
            }
            if(yh.compareTo(sk)==1){
                throw new ServiceException("优惠金额大于收款金额！");
            }
            if(sk.compareTo(ys)==1){
                throw new ServiceException("收款金额大于应收金额！");
            }
            if(sk.compareTo(new BigDecimal("0"))==0){
                if(!"赊销".equals(jiesuanShoukuan.getSkfs())){
                    throw new ServiceException("收款金额不能为0.00  ---收款方式（赊销）除外");
                }
            }
        if("订单出库".equals(jiesuanShoukuan.getYwType())||"成品出库".equals(jiesuanShoukuan.getYwType())) {
              if (StringUtils.isNotEmpty(jiesuanShoukuan.getYwId())) {
                  CcChukuMain chuku = chukuMainMapper.selectCcChukuMainByDanHao(jiesuanShoukuan.getYwId());
                  if (chuku != null) {
                      BigDecimal totalPrice = chuku.getTotalPrice();
                      if (StringUtils.isNotEmpty(map)) {
                          BigDecimal skmoney = new BigDecimal(String.valueOf(map.get("skmoney")));
                          BigDecimal yhmoney = new BigDecimal(String.valueOf(map.get("yhmoney")));
                          BigDecimal zMoney = skmoney.add(yhmoney).add(jiesuanShoukuan.getSkMoney()).add(jiesuanShoukuan.getYhMoney());
                          if (zMoney.compareTo(totalPrice) == 1) {
                              throw new ServiceException("支出金额大于原材料入库总金额！");
                          } else if (totalPrice.compareTo(zMoney) == 0) {
                              chuku.setJzFlag("已结清");
                          } else if (totalPrice.compareTo(zMoney) == 1 && meoney.compareTo(new BigDecimal("0")) == 1) {
                              chuku.setJzFlag("部分结清");
                          } else {
                              chuku.setJzFlag("未结清");
                          }
                      } else {
                          if (meoney.compareTo(totalPrice) == 1) {
                              throw new ServiceException("支出金额大于原材料入库总金额！");
                          } else if (totalPrice.compareTo(meoney) == 0) {
                              chuku.setJzFlag("已结清");
                          } else if (totalPrice.compareTo(meoney) == 1 && meoney.compareTo(new BigDecimal("0")) == 1) {
                              chuku.setJzFlag("部分结清");
                          } else {
                              chuku.setJzFlag("未结清");
                          }
                      }
                      chukuMainMapper.updateCcChukuMain(chuku);
                  } else {
                      throw new ServiceException("未找到该业务单！" + jiesuanShoukuan.getYwId());
                  }
              }else{
                  throw new ServiceException("请选择业务单号不能为空！");
              }
                /* CcOrderInfo orderInfo = ccOrderInfoMapper.selectCcOrderInfoById(Long.parseLong(jiesuanShoukuan.getYwId2()));
            if(orderInfo!=null){
                Map<String, String> mapO = chukuMainMapper.tJChukuOrderPrice(jiesuanShoukuan.getYwId2());
                BigDecimal chukuOrderPrice=new BigDecimal( String.valueOf(mapO.get("chukuOrderPrice")));
                BigDecimal orderPrice = orderInfo.getPrice();
                if(chukuOrderPrice.compareTo(orderPrice)==1) {
                    throw new ServiceException("出库单总金额大于订单总金额！");
                }else if(chukuOrderPrice.compareTo(orderPrice)==0){
                    chuku.setJzFlag("已结清");
                }else if(orderPrice.compareTo(chukuOrderPrice)==1 && chukuOrderPrice.compareTo(new BigDecimal("0"))==1){
                    chuku.setJzFlag("部分结清");
                }else {
                    chuku.setJzFlag("未结清");
                }
            }else {
                throw new ServiceException("订单不存在！");
            }*/
            }
         jiesuanShoukuan.setCreateTime(DateUtils.getNowDate());
        return jiesuanShoukuanMapper.insertJiesuanShoukuan(jiesuanShoukuan);
    }

    /**
     * 修改收款记账
     * 
     * @param jiesuanShoukuan 收款记账
     * @return 结果
     */
    @Override
    public int updateJiesuanShoukuan(JiesuanShoukuan jiesuanShoukuan)
    {
        jiesuanShoukuan.setUpdateTime(DateUtils.getNowDate());
        return jiesuanShoukuanMapper.updateJiesuanShoukuan(jiesuanShoukuan);
    }

    /**
     * 批量删除收款记账
     * 
     * @param ids 需要删除的收款记账主键
     * @return 结果
     */
    @Override
    public int deleteJiesuanShoukuanByIds(Long[] ids)
    {
        return jiesuanShoukuanMapper.deleteJiesuanShoukuanByIds(ids);
    }

    /**
     * 删除收款记账信息
     * 
     * @param id 收款记账主键
     * @return 结果
     */
    @Override
    public int deleteJiesuanShoukuanById(Long id)
    {
        return jiesuanShoukuanMapper.deleteJiesuanShoukuanById(id);
    }

    @Override
    public BigDecimal selectJsSurplusShoukuan(String ywId) {
        Map<String, String> map = jiesuanShoukuanMapper.selectTjShowKuanByYwId(ywId);
        if(StringUtils.isNotEmpty(map)){
            BigDecimal ysMoney=new BigDecimal( String.valueOf(map.get("ysmoney")));
            BigDecimal yhmoney=new BigDecimal( String.valueOf(map.get("yhmoney")));
            BigDecimal skmoney=new BigDecimal( String.valueOf(map.get("skmoney")));
            return skmoney.add(yhmoney);
        }else {
            return new BigDecimal("0.00");
        }
    }
}
